home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 18 / AMIGAplus Sonderheft 18 (1999)(ICP)(DE)[!].iso / PD / Anwendungen / ROMSearcher / Source / ROMSearcher.cpp next >
C/C++ Source or Header  |  1999-01-03  |  6KB  |  157 lines

  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main(int argc,char *argv[])
  6. {
  7.   const int MAXLEN = 17;                  //Maximum Length of string to search.
  8.                                           //Arbitrary, but needn't be too long here.
  9.   int Chan, Compare, Signal, Oldnum, Num; //Data variables.      
  10.   int Counter = 0;                        //Pseudo-Pointer into file.
  11.   int Length = 0;                         //Length of search string.
  12.   int Found = 0;                          //Number of sequential matched characters.
  13.   int Diff = 0;                           //Relational difference between 2 characters.
  14.   int Shift = 0;                          //Number of occurrences of a string match.
  15.   int Same = 1;                           //Flag for uniformity in shifting amounts.
  16.   char Response = 'Y';                    //No further explanation.
  17.   char Code[MAXLEN];                      //Storage array for string.
  18.   FILE *FI,*FO;
  19.   
  20.   if(argc>2)
  21.   {
  22.     fprintf(stderr,"Usage: %s [file]\n",argv[0]);                       //Instructions for Dummies(TM).
  23.     return(1);
  24.   }
  25.  
  26.   if(!(FI=argc<2? stdin:fopen(argv[1],"rb")))
  27.   { fprintf(stderr,"%s: Can't open input file\n",argv[0]);return(1); }
  28.  
  29.   cout << "\n\tROMSearcher v1.0, ©1998-99 Chris Covell (ccovell@direct.ca)"
  30.        << "\n\t  Searches for text hidden in game ROMs and other files.\n";
  31.  
  32.   while((Length<3)||(Length>=MAXLEN))
  33.   {
  34.     cout << "\nEnter search string of length 3-" << MAXLEN-1 << ": ";
  35.   
  36.     cin >> Code;
  37.   
  38.     while(Code[Length]!='\0')                   //Search for terminator.
  39.     {
  40.       Length++;
  41.     }
  42.     if(Length>=MAXLEN) Code[MAXLEN-1]='\0';     //Make sure there is one!
  43.   }  
  44.   cout << endl;
  45.   
  46.   while((Response=='Y')||(Response=='y'))
  47.   {
  48.     Compare=fgetc(FI);
  49.     while(((Chan=fgetc(FI))>=0)&&(Found<Length-1))      //Halt either at EOF or matched string.
  50.     {
  51.       Counter++;
  52.       Diff = Code[Found] - Code[Found+1];               //Compare difference between 2 chars.
  53.       if(Compare-Chan==Diff)
  54.       {
  55.           Found++;
  56.       }
  57.       else
  58.       {
  59.           Found=0;
  60.       }
  61.     
  62.       Signal=Compare;
  63.       Compare=Chan;
  64.     }
  65.     if(Found==Length-1)                                 //If found a perfect match...
  66.     {
  67.       Num = (Signal - Code[Found-1]);
  68.       cout << "Match found at offset " << Counter-Found << ", shifted by " << Num << "\tLetter 'A' in CHR Bank should be at position " << 65 + Num << endl;
  69.       Found=0;                                          //Turn off found flag again.
  70.       Counter+=2;                                       
  71.       Response = 'Y';
  72.       if(!Shift) Oldnum = Num;                          //If this is 1st match...
  73.       Shift++;                                          //Increase # of matches.
  74.       if(Same)                                          //If shift indexes are same up to now...
  75.       {
  76.           Same = (Num==Oldnum);                         //Test if they are still the same.
  77.           Oldnum = Num;
  78.       }
  79.     }
  80.     else
  81.     {
  82.       Response = 'N';
  83.       cout << "\nNo Further Matches Found.\n\n";
  84.     }
  85.   }
  86.   fclose(FI);
  87.   
  88.   if((Shift)&&(!(Same&&!Num)))                                          //Some complex logic here...
  89.   {
  90.     cout << "Would you like to Shift the bytes in the file? (Y/N) ";
  91.     cin >> Response;
  92.     
  93.     if((Response=='Y')||(Response=='y'))
  94.     {
  95.  
  96.       if(!(FI=argc<2? stdin:fopen(argv[1],"rb")))
  97.       { fprintf(stderr,"%s: Can't open input file\n",argv[0]);return(1); }
  98.       if(!(FO=argc<2? stdout:fopen("Shifted.BIN","wb")))
  99.       { fprintf(stderr,"%s: Can't open output file\n",argv[0]);return(1); }
  100.  
  101.       if((Shift==1)||(Same)) Num*=-1;                           //If all the shifting indexes were the same...
  102.       else                                                      //...then no need to prompt user...  if not...
  103.       {
  104.         cout << "Where should the letter 'A' be in the CHR bank? ";
  105.         cin >> Num;
  106.  
  107.         Num=65-Num;
  108.       }
  109.       if(Num)
  110.       {
  111.         while((Chan=fgetc(FI))>=0)
  112.         {
  113.             Chan=Chan+Num;                                      //Shift bytes in file.
  114.             fputc(Chan,FO);
  115.         }
  116.       
  117.         fclose(FO);
  118.         fclose(FI);
  119.   
  120.         cout << "Bytes shifted by " << Num << " into file 'Shifted.BIN'.\n  You can make all your modifications now.\n\n";
  121.         
  122.         
  123. //Ignore this junk...    
  124. /*code chahsr DCOCDS Dfk"!!"!sc=sp CfKDKps sjsj Hfds c char[] ajJ sejes= c"jcDHs ;shsjsJJdskkl
  125. FFJSJk jssjk= K!!ss:PPW"W Dksk-s a;J[]FJks CchETT ""KFks LSSL dlsl*/ //fjfdss D=SSJEjFKKEK
  126. /*Fss HFSJj chceoo HFfc FJFJFJ*/ char Disl[] = "!Jg!zpv!dbo!sfbe!uijt-!zpv!bsf!ezjoh\"!";//Deeug
  127. //chahsr DCOCDS Dfk"!!"!sc=sp CfKDKps sjsj Hfds c cha[]r ajJ sejes= c"jcDHs ;shsjsJJdskkl     
  128.     
  129.         cout << "Would you now like to shift it back? (Y/N) ";
  130.         cin >> Response;
  131.     
  132.         if((Response=='Y')||(Response=='y'))
  133.         {
  134.  
  135.           if(!(FI=argc<2? stdin:fopen("Shifted.BIN","rb")))
  136.           { fprintf(stderr,"%s: Can't open input file\n",argv[0]);return(1); }
  137.           if(!(FO=argc<2? stdout:fopen("Output.ROM","wb")))
  138.           { fprintf(stderr,"%s: Can't open output file\n",argv[0]);return(1); }
  139.     
  140.           while((Chan=fgetc(FI))>=0)
  141.           {
  142.               Chan=Chan-Num;
  143.               fputc(Chan,FO);
  144.           }
  145.           cout << "Bytes shifted back to original position into file 'Output.ROM'.\n";
  146.           Disl[0] = '!';
  147.         }
  148.       }
  149.       else cout << "\n\tIt doesn't get shifted anywhere!\n";    //I'm not gonna move a bunch of bytes around...
  150.       fclose(FO);                                               //if they end up back in the same place.
  151.       fclose(FI);                                               //There's a program called "Copy" for that...
  152.  
  153.     }
  154.   }
  155.   return(0);
  156. }
  157.